package com.cxb.java;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * @Classname SharedReentrantLock
 * @Date 2023/4/1 10:32
 * @Created by Administrator
 * @Description 测试可重入锁（可以多次获得锁不会被阻塞，释放时也需释放多把锁）
 */
public class SharedReentrantLock1 implements Runnable {
    private InterProcessMutex lock;//可重入锁实现类
    private String lockPAth = "/lock/shareLock";
    private int i;
    private String clientName;
    //zookeeper集群地址
    public static final String ZOOKEEPER_STRING = "127.0.0.1:2181";

    public SharedReentrantLock1(CuratorFramework client, String clientName) {
        lock = new InterProcessMutex(client, lockPAth);
        this.clientName = clientName;
    }

    public void run() {
        try {
            Thread.sleep((new java.util.Random().nextInt(2000)));
            lock.acquire();  //增加第一把锁
            if (lock.isAcquiredInThisProcess()) {
                System.out.println(clientName + " 获得锁");
                i++;
            }
            lock.acquire();  //增加第二把锁
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                System.out.println(clientName + "释放第一把锁");
                lock.release();
                System.out.println(clientName + "释放第二把锁");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        CuratorFramework client = CuratorFrameworkFactory.newClient(ZOOKEEPER_STRING, new ExponentialBackoffRetry(1000, 3));
        client.start();
        //启动100个线程进行测试
        for (int i = 0; i < 100; i++) {
            SharedReentrantLock1 sharedReentrantLock = new SharedReentrantLock1(client, "第" + i + "个客户端：");
            Thread thread = new Thread(sharedReentrantLock);
            thread.start();
        }
    }

}
